The Bug Class:
The Bug class has three methods that specify how bugs move and turn.
1. canMove()
Public boolean canMove()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return false; //If the grid does not exist the bug can not move
Location loc = getLocation(); //Gets the current location of the bug
Location next = loc.getAdjacentLocation(getDirection()); //Gets the location of the square
//next to the current location of
//the bug in the direction the bug
//is facing. (The destination of
//the bug)
if (!gr.isValid(next))
return false; //If Location next is not valid the bug can not move.
Actor neighbor = gr.get(next);
return (neighbor == null) || (neighbor instanceOf Flower); //The bug can only move if the
//distination is empty or holding
//a flower.
}
2. move()
public void move()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return; //kicks out of the program if the grid does not exist
Location loc = getLoaction(); //Gets the current location of the bug
Location next = loc.getAdjacentLocation(getDirection()); //Gets the location of the square
//next to the current location of
//the bug in the direction the bug
//is facing.
if (gr.isValid(next))
moveTo(next); //moves the bug to the next space if it is valid
else
removeSelfFromGrid(); //moves the bug from the grid if the next space is not valid
Flower flower = new Flower(getColor());
flower.putSelfInGrid(gr, loc); //makes a flower the color of the bug and places it in the
//space of the bug after it moves.
}
3. turn()
public void turn()
{
setDirection(getDirection() + Location.HALF_RIGHT); //Turns the bug 45 degrees to the right
//of the current direction of the bug.
}
These methods are built into the Bug actor and can be imported and used using this:
import info.gridworld.actor.Bug;
The methods can be rewritten and overwrited to fit your needs but those are the default methods. They are used in the act method to control the behavior of the bug (the way the bug moves and acts).
This is an example of a basic act method:
public void act()
{
if(canMove()) //If canMove is true (If the bug can move) then move the bug
move();
else
turn(); //If it can't than turn the bug.
}
The act method can be altered to effect the behavior and the movement of the bug
This act method use two instance variables, sideLength and steps to make the bug move in a square pattern:
public void act()
{
if (steps < sideLength && canMove()) //this is a loop that makes the bug move the number of
{ //spaces given by sideLength.
move();
steps++;
}
else //This turns the bug 90 degrees (45 per turn) when the bug moved sideLength
//spaces and starts the process over from the beginning.
{
turn();
turn();
steps = 0;
}
}
This code can be changed to make the bug move in many different kinds of patterns.
The Location Class:
The location class can be imported and used using:
import info.gridworld.grid.Location;
Every actor that is on the grid has it's own location in the grid. The Location class defines the coordinates for an actor's position in a grid and provides methods that determine relationships between locations and compass directions.
Compass directions:
North = 0
East = 90
South = 180
West = 270
North east = 45
South east = 135
south west = 225
north west = 315
Compass directions are stored as finial intigers and can be used with their name or their compass direction in degrees (NORTHWEST or 315)
The Location class also stores commonly used turn angles at finial intigers:
public static final int LEFT = -90;
public static final int RIGHT = 90;
public static final int HALF_LEFT = -45;
public static final int HALF_RIGHT = 45; //This is used in the turn method of the Bug actor.
public static final int FULL_CIRCLE = 360;
public static final int HALF_CIRCLE = 180;
public static final int AHEAD = 0;
A location has a row and a column which are the parameters of the Location constructor
public Location(int r, int c)
Two accessor methods return the row and column for a location
public int getRow() //returns the row for a location (loc.getRow())
public int getCol() //returns the column for a location (loc.getCol())
Two other methods give info about the relationships between locations and directions
1. public Location getAdjacentLocation(int direction)
This returns the adjacent location in the direction of int direction
Location loc1 = new Location(5,7)
Location loc2 = loc1.getAdjacentLocation(Location.WEST);
//loc2 = (5, 6). Is is next too loc1 on the west side.
2. public int getDirectionToward(Location target)
This returns the direction from a location toward a target
Location loc1 = new Location(5,7);
int dir = loc1.getDirectionToward(new Location(6, 8));
//dir = 135 degrees.
The Bug class has three methods that specify how bugs move and turn.
1. canMove()
Public boolean canMove() { Grid<Actor> gr = getGrid(); if (gr == null) return false; //If the grid does not exist the bug can not move Location loc = getLocation(); //Gets the current location of the bug Location next = loc.getAdjacentLocation(getDirection()); //Gets the location of the square //next to the current location of //the bug in the direction the bug //is facing. (The destination of //the bug) if (!gr.isValid(next)) return false; //If Location next is not valid the bug can not move. Actor neighbor = gr.get(next); return (neighbor == null) || (neighbor instanceOf Flower); //The bug can only move if the //distination is empty or holding //a flower. }2. move()public void move() { Grid<Actor> gr = getGrid(); if (gr == null) return; //kicks out of the program if the grid does not exist Location loc = getLoaction(); //Gets the current location of the bug Location next = loc.getAdjacentLocation(getDirection()); //Gets the location of the square //next to the current location of //the bug in the direction the bug //is facing. if (gr.isValid(next)) moveTo(next); //moves the bug to the next space if it is valid else removeSelfFromGrid(); //moves the bug from the grid if the next space is not valid Flower flower = new Flower(getColor()); flower.putSelfInGrid(gr, loc); //makes a flower the color of the bug and places it in the //space of the bug after it moves. }3. turn()public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); //Turns the bug 45 degrees to the right //of the current direction of the bug. }These methods are built into the Bug actor and can be imported and used using this:The methods can be rewritten and overwrited to fit your needs but those are the default methods. They are used in the act method to control the behavior of the bug (the way the bug moves and acts).
This is an example of a basic act method:
public void act() { if(canMove()) //If canMove is true (If the bug can move) then move the bug move(); else turn(); //If it can't than turn the bug. }The act method can be altered to effect the behavior and the movement of the bugThis act method use two instance variables, sideLength and steps to make the bug move in a square pattern:
public void act() { if (steps < sideLength && canMove()) //this is a loop that makes the bug move the number of { //spaces given by sideLength. move(); steps++; } else //This turns the bug 90 degrees (45 per turn) when the bug moved sideLength //spaces and starts the process over from the beginning. { turn(); turn(); steps = 0; } }This code can be changed to make the bug move in many different kinds of patterns.The Location Class:
The location class can be imported and used using:
Every actor that is on the grid has it's own location in the grid. The Location class defines the coordinates for an actor's position in a grid and provides methods that determine relationships between locations and compass directions.
Compass directions:
North = 0
East = 90
South = 180
West = 270
North east = 45
South east = 135
south west = 225
north west = 315
Compass directions are stored as finial intigers and can be used with their name or their compass direction in degrees (NORTHWEST or 315)
The Location class also stores commonly used turn angles at finial intigers:
A location has a row and a column which are the parameters of the Location constructor
Two accessor methods return the row and column for a location
Two other methods give info about the relationships between locations and directions
1. public Location getAdjacentLocation(int direction)
This returns the adjacent location in the direction of int direction
2. public int getDirectionToward(Location target)
This returns the direction from a location toward a target